package com.ls.bpmn.dto;

import com.ls.common.Empty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.time.DateFormatUtils;
import org.camunda.bpm.engine.history.HistoricTaskInstance;
import org.camunda.bpm.engine.task.Task;

import java.util.Date;

/**
 * @Author lisheng
 * @Date 2025/05/07 10:05
 **/
@Data
@NoArgsConstructor
public class TaskDTO {

    /** 任务ID */
    private String taskId;

    /** 工单所在流程环节ID */
    private String nodeId;

    /** 工单所在流程环节 */
    private String nodeName;

    /** 签收人 */
    public String assignee;

    /** 签收人姓名 */
    public String assigneeName = "";

    /** 任务办结时间 */
    private String startTime;

    /** 任务办结时间 */
    private String endTime;

    /** 任务是否挂起 */
    private boolean suspended;

    /** 流程实例ID */
    private String processInstanceId;

    /** 业务数据ID */
    private String businessKey;

    /** 执行实例ID */
    private String executionId;

    /** 流程定义ID */
    private String processDefinitionId;

    /** 流程定义key */
    private String processDefinitionKey;

    /** 流程定义名称 */
    private String processDefinitionName;

    public TaskDTO(Task task, String processDefinitionKey, String processDefinitionName){
        this.nodeId = task.getTaskDefinitionKey();
        this.nodeName = task.getName();
        this.taskId = task.getId();
        this.assignee = task.getAssignee();
        this.processDefinitionName = processDefinitionName;
        this.processDefinitionKey = processDefinitionKey;
        this.processDefinitionId = task.getProcessDefinitionId();
        this.processInstanceId = task.getProcessInstanceId();
        this.executionId = task.getExecutionId();
        this.startTime = formatDate(task.getCreateTime());
        this.suspended = task.isSuspended();
    }

    public TaskDTO(HistoricTaskInstance task, String processDefinitionName){
        this.nodeId = task.getTaskDefinitionKey();
        this.nodeName = task.getName();
        this.taskId = task.getId();
        this.assignee = task.getAssignee();
        this.processDefinitionName = processDefinitionName;
        this.processDefinitionKey = task.getProcessDefinitionKey();
        this.processDefinitionId = task.getProcessDefinitionId();
        this.processInstanceId = task.getProcessInstanceId();
        this.executionId = task.getExecutionId();
        this.startTime = formatDate(task.getStartTime());
        this.endTime = formatDate(task.getEndTime());
    }

    public static String formatDate(Date date){
        return Empty.isEmpty(date) ? "" : DateFormatUtils.format(date,"yyyy-MM-dd HH:mm:ss");
    }
}
